package com.tx.controller;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tx.entity.User;
import com.tx.service.IUserService;
import com.tx.util.AjaxResult;
import com.tx.util.SecurityUtils;
import com.tx.util.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private IUserService userService;


    @GetMapping("/list")
    public String list(Model model) {
        return "/user/user_list";
    }



    @GetMapping("/listPage")
    @ResponseBody
    public TableDataInfo listPage(String searchParams, int page, int limit) {

        User userSearch = JSONObject.parseObject(searchParams,User.class);
        Page<User> pageObj = new Page<>(page, limit);
        Page<User> userPage = userService.selectUserByPage(pageObj,userSearch);

        return TableDataInfo.getDataTable(userPage.getRecords(),userPage.getTotal());
    }


    @GetMapping("/toAdd")
    public String toAdd(Model model) {
        return "/user/user_add";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestBody User user) {

        User userFlag = userService.selectUserByUserName(user.getUsername());

        if (ObjectUtil.isNotNull(userFlag)){
            return AjaxResult.error("用户已存在,请更换用户名");
        }

        int row = userService.registerUser(user);
        return AjaxResult.toAjax(row);
    }


    @GetMapping("/toEdit/{id}")
    public String toEdit(@PathVariable("id") Integer id, Model model) {

        User user = userService.selectUserById(id);
        model.addAttribute("user",user);

        return "/user/user_edit";
    }


    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestBody User user) {
        int row = userService.updateUser(user);
        return AjaxResult.toAjax(row);
    }


    @PostMapping("/delete/{id}")
    @ResponseBody
    public AjaxResult deleteById(@PathVariable("id") Integer id) {
        int row = userService.deleteUserById(id);
        return AjaxResult.toAjax(row);
    }


    @RequestMapping("/toSetting")
    public String toSetting(Model model) {
        User user = SecurityUtils.getUser();
        user = userService.selectUserByUserName(user.getUsername());
        model.addAttribute("user",user);
        return "/user/user_setting";
    }


    @RequestMapping("/toPassword")
    public String toPassword(Model model) {
        User user = SecurityUtils.getUser();
        user = userService.selectUserByUserName(user.getUsername());
        model.addAttribute("user",user);
        return "/user/user_password";
    }



    @PostMapping("/updateUserSetting")
    @ResponseBody
    public AjaxResult updateUserSetting(@RequestBody User user) {
        int row = userService.updateUser(user);
        return AjaxResult.toAjax(row);
    }


    @PostMapping("/updatePassword")
    @ResponseBody
    public AjaxResult updatePassword(@RequestBody Map<String,String> requestMap) {

        User user = SecurityUtils.getUser();
        //获取参数
        String old_password = requestMap.get("old_password");
        String new_password = requestMap.get("new_password");
        String again_password = requestMap.get("again_password");

        if (StringUtils.isEmpty(old_password) || StringUtils.isEmpty(new_password) || StringUtils.isEmpty(again_password)){
            return AjaxResult.error("信息不能为空");
        }

        //校验老密码是否正确
        if (!user.getPassword().equals(old_password)){
            return AjaxResult.error("旧密码错误");
        }

        //判断两次密码是否一致
        if (!new_password.equals(again_password)){
            return AjaxResult.error("两次密码不一致");
        }

        //更新密码
        user.setUserPassword(new_password);
        int row = userService.updateUser(user);

        return AjaxResult.toAjax(row);
    }

}
